package com.zgl.gemduel.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 设置键值
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 获取键值
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 设置键值并设置过期时间
    public void setWithExpire(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 删除键
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}